package pl.th.permission.plugin.node.annotation;

import java.util.Objects;

import pl.th.permission.plugin.Utils;

public class AnyAnnotation extends CallbackAnnotation{

    protected int requestCode;

    public AnyAnnotation(AnnType type) {
        super(type);
    }

    public int getRequestCode() {
        if (requestCode == 0)
            throw new RuntimeException("requestCode is necessary for the annotation: " + mAnnType);
        return requestCode;
    }

    @Override
    public void visit(String name, Object value) {
        super.visit(name, value);
        if (Utils.getRequestCodeName().equals(name)) {
            requestCode = (int) value;
        }
    }

    @Override
    public boolean checkAnnotationType(AnnType type) {
        return type == AnnType.ON_ANY_PERMISSION_GRANTED
                || type == AnnType.ON_ANY_PERMISSION_DENIED;
    }

    @Override
    public String getMethodDesc() {
        return Utils.METHOD_DESC_ANY_CALLBACK;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        AnyAnnotation that = (AnyAnnotation) o;
        return requestCode == that.requestCode;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), requestCode);
    }

    @Override
    public String toString() {
        return "AnyAnnotation{" +
                "requestCode=" + requestCode +
                "} " + super.toString();
    }
}
